﻿using System;
using System.Collections.Generic;
using System.IO;
using ProjScan.Modules;
using ProjScan.Tools;
using UnityEditor;
using UnityEngine;

namespace ProjScan.Submodules.CS_Check
{
	// Token: 0x0200019B RID: 411
	internal class AssemblyManager : Singleton<AssemblyManager>
	{
		// Token: 0x06000DE4 RID: 3556 RVA: 0x0003EAB0 File Offset: 0x0003CCB0
		private List<AssemblyDetails> GetFilteredAssemblyDetails()
		{
			List<AssemblyDetails> list = new List<AssemblyDetails>();
			foreach (string text in ApiCompatibilityUtils.Instance.GetAssemblyPaths())
			{
				bool flag = !text.EndsWith("Assembly-CSharp-Editor.dll");
				if (flag)
				{
					AssemblyDetails assemblyDetail = this.GetAssemblyDetail(text);
					bool flag2 = assemblyDetail != null;
					if (flag2)
					{
						list.Add(assemblyDetail);
					}
				}
			}
			return list;
		}

		// Token: 0x06000DE5 RID: 3557 RVA: 0x0003EB20 File Offset: 0x0003CD20
		public string getDefaultAssemblyName()
		{
			return Path.GetFileNameWithoutExtension("Assembly-CSharp.dll");
		}

		// Token: 0x06000DE6 RID: 3558 RVA: 0x0003EB3C File Offset: 0x0003CD3C
		public void DeleteTempDirectory()
		{
			foreach (string text in this.tempPaths)
			{
				bool flag = !string.IsNullOrEmpty(text) && Directory.Exists(text);
				if (flag)
				{
					try
					{
						Directory.Delete(text, true);
					}
					catch (Exception exception_)
					{
						ScanLog.Instance.LogException(exception_, "DeleteTmp", "");
					}
				}
			}
		}

		// Token: 0x06000DE7 RID: 3559 RVA: 0x0003EBD8 File Offset: 0x0003CDD8
		public string[] GetAssemblyNamesList()
		{
			int num = Singleton<ScanInfo>.Instance.unityVersion[0];
			int num2 = Singleton<ScanInfo>.Instance.unityVersion[1];
			bool flag = num < 2017 || (num == 2017 && num2 <= 2);
			List<AssemblyDetails> list;
			if (flag)
			{
				list = this.GetLegacyAssemblyDetails();
			}
			else
			{
				list = this.GetFilteredAssemblyDetails();
			}
			List<string> list2 = new List<string>();
			foreach (AssemblyDetails assemblyDetails in list)
			{
				list2.Add(assemblyDetails.name);
			}
			return list2.ToArray();
		}

		// Token: 0x06000DE8 RID: 3560 RVA: 0x0003EC9C File Offset: 0x0003CE9C
		public List<AssemblyDetails> GetAssemblyDetails()
		{
			int num = Singleton<ScanInfo>.Instance.unityVersion[0];
			int num2 = Singleton<ScanInfo>.Instance.unityVersion[1];
			bool flag = num < 2017 || (num == 2017 && num2 <= 2);
			List<AssemblyDetails> result;
			if (flag)
			{
				result = this.GetLegacyAssemblyDetails();
			}
			else
			{
				bool flag2 = num < 2018 || (num == 2018 && num2 <= 1);
				if (flag2)
				{
					result = this.GetFilteredAssemblyDetails();
				}
				else
				{
					result = this.GetCompiledAssemblyDetails();
				}
			}
			return result;
		}

		// Token: 0x06000DE9 RID: 3561 RVA: 0x0003ED28 File Offset: 0x0003CF28
		private List<AssemblyDetails> GetLegacyAssemblyDetails()
		{
			List<AssemblyDetails> list = new List<AssemblyDetails>();
			string text = Application.dataPath + "/../Library/ScriptAssemblies";
			bool flag = !Directory.Exists(text);
			List<AssemblyDetails> result;
			if (flag)
			{
				result = null;
			}
			else
			{
				new List<string>();
				bool flag2 = File.Exists(text + "/Assembly-CSharp.dll");
				if (flag2)
				{
					AssemblyDetails assemblyDetail = this.GetAssemblyDetail(text + "/Assembly-CSharp.dll");
					list.Add(assemblyDetail);
				}
				bool flag3 = File.Exists(text + "/Assembly-CSharp-firstpass.dll");
				if (flag3)
				{
					AssemblyDetails assemblyDetail2 = this.GetAssemblyDetail(text + "/Assembly-CSharp-firstpass.dll");
					list.Add(assemblyDetail2);
				}
				result = list;
			}
			return result;
		}

		// Token: 0x06000DEA RID: 3562 RVA: 0x0003EDD4 File Offset: 0x0003CFD4
		private List<AssemblyDetails> GetCompiledAssemblyDetails()
		{
			List<AssemblyDetails> list = new List<AssemblyDetails>();
			string uniqueTempPathInProject = FileUtil.GetUniqueTempPathInProject();
			this.tempPaths.Add(uniqueTempPathInProject);
			string[] array = null;
			ScanLog.Instance.LogInfo(ScanLog.LogLevel.INFO, Module.CS_Check, ScanLog.Stage.s2, "GetAssmblyInfosCompiled:asmOutputFolder", uniqueTempPathInProject);
			try
			{
				array = ApiCompatibilityUtils.Instance.CompileAssemblies(uniqueTempPathInProject);
			}
			catch (Exception ex)
			{
				ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, Module.CS_Check, ScanLog.Stage.s2, "GetAssmblyInfosCompiled", string.Concat(new string[]
				{
					(ex.GetType() == null) ? null : ex.GetType().ToString(),
					": ",
					ex.Message,
					"\n",
					ex.StackTrace
				}));
			}
			foreach (string text in array)
			{
				ScanLog.Instance.LogInfo(ScanLog.LogLevel.INFO, Module.CS_Check, ScanLog.Stage.s2, "GetAssmblyInfosCompiled:compiledAssemblyPath", text);
				AssemblyDetails assemblyDetail = this.GetAssemblyDetail(text);
				bool flag = assemblyDetail != null;
				if (flag)
				{
					list.Add(assemblyDetail);
				}
			}
			return list;
		}

		// Token: 0x06000DEB RID: 3563 RVA: 0x0003EEF4 File Offset: 0x0003D0F4
		private AssemblyDetails GetAssemblyDetail(string string_0)
		{
			string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(string_0);
			string assemblyDefinitionFilePath = ApiCompatibilityUtils.Instance.GetAssemblyDefinitionFilePath(fileNameWithoutExtension);
			ScanLog.Instance.LogInfo(ScanLog.LogLevel.INFO, Module.CS_Check, ScanLog.Stage.s2, "GetAssemblyInfoFromAssemblyPath:asmDefPath", assemblyDefinitionFilePath);
			AssemblyDetails assemblyDetails = new AssemblyDetails();
			assemblyDetails.name = fileNameWithoutExtension;
			bool flag = assemblyDefinitionFilePath != null;
			if (flag)
			{
				assemblyDetails.asmDefPath = ToolsHelper.DirConvert(assemblyDefinitionFilePath);
				char[] separator = new char[]
				{
					'/'
				};
				string[] array = assemblyDefinitionFilePath.Split(separator);
				bool flag2 = array.Length > 2 && array[0].Equals("Packages");
				if (flag2)
				{
					ScanLog.Instance.LogInfo(ScanLog.LogLevel.INFO, Module.CS_Check, ScanLog.Stage.s2, "GetAssemblyInfoFromAssemblyPath:folders[0]", array[0]);
					assemblyDetails.relativePath = Path.Combine(array[0], array[1]).Replace("\\", "/");
					assemblyDetails.asmType = AssemblyDetails.AsmType.InPackages;
				}
				else
				{
					assemblyDetails.asmType = AssemblyDetails.AsmType.UserDefined;
				}
			}
			else
			{
				bool flag3 = assemblyDetails.name.StartsWith(this.getDefaultAssemblyName());
				if (flag3)
				{
					assemblyDetails.asmType = AssemblyDetails.AsmType.Default;
				}
				else
				{
					Debug.LogError("Assembly Definition cannot be found for " + assemblyDetails.name);
				}
			}
			ScanLog.Instance.LogInfo(ScanLog.LogLevel.INFO, Module.CS_Check, ScanLog.Stage.s2, "GetAssemblyInfoFromAssemblyPath:asmType", assemblyDetails.asmType.ToString());
			assemblyDetails.filePath = string_0;
			return assemblyDetails;
		}

		// Token: 0x040008B7 RID: 2231
		private List<string> tempPaths = new List<string>();
	}
}
